package com.xique.blelock.view;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.clj.fastble.BleManager;
import com.hss01248.dialog.ActivityStackManager;
import com.ivin.widget.DialogOfProgress;
import com.xique.blelock.Utils.ACache;
import com.xique.blelock.Utils.BleUtils;
import com.xique.blelock.Utils.MyActivityManager;
import com.xique.blelock.Utils.Stringkey;
import com.xique.blelock.Utils.SystemBarUtils;
import com.xique.blelock.Utils.ToastUtil;
import com.xique.blelock.R;
import com.xique.blelock.model.UserInfo;

import butterknife.OnClick;


/**
 * Created by wxm on 2017/3/7.
 */

public class BaseActivity extends AppCompatActivity {

    private DialogOfProgress dialog;

    protected UserInfo userInfo;
    protected int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;


    protected void initView(){}

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baselayout);
        SystemBarUtils.setStatusBar(this, false, true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }

        userInfo = (UserInfo) ACache.get(this).getAsObject(Stringkey.USERInfo);
        MyActivityManager.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyActivityManager.removeActivity(this);
    }

    public void showToast(Context context, String content) {
        ToastUtil.getInstance().showToast(context, content);
    }

    public void showToast(Context context, int content) {
        ToastUtil.getInstance().showToast(context, context.getString(content));
    }

    //初始化蓝牙设备
    protected void initBle() {
        BleManager.getInstance().init(getApplication());
        boolean supportble = BleManager.getInstance().isSupportBle();
        if(!supportble){
            showToast(this, "当前不支持蓝牙");
        }

        boolean isblueenable = BleManager.getInstance().isBlueEnable();
        if(!isblueenable){
            showToast(this, "还没有开启蓝牙");
            BleManager.getInstance().enableBluetooth();
        }



        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)
                .setConnectOverTime(30000)
                .setOperateTimeout(5000);

    }



    /**
     * 点击屏幕隐藏键盘
     */
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        ScreenUtil.hideKeybord(this);
//        return super.onTouchEvent(event);
//    }
//
//    @Override
//    protected void onDestroy() {
//        ScreenUtil.hideKeybord(this);
//        super.onDestroy();
//    }
//
//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//        super.onConfigurationChanged(newConfig);
//    }
//
    public void showProgressDialog() {
        if(dialog != null){
            dialog.dismiss();
            dialog = null;
        }
        dialog = new DialogOfProgress(this, true);
        dialog.show();
    }

    public void hideProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
    
    
//    public void judgeResponse(retrofit2.Response response) {
//        if (response.body() != null) {
//            FxCommonResult<Object> body = (FxCommonResult<Object>) response.body();
//            if (!body.isSuccess()) {
//                String code = body.getCode();
//                if (code!=null && code.equals("500")) {
//                    Intent intent = new Intent(this, LoginActivity.class);
//                    intent.putExtra("justLogin", 1);
//                    startActivity(intent);
//                }
//            }
//        }
//    }
//
//    public void showOnFailure() {
//        if (NetUtil.isConnected(this)) {
//            showToast(R.string.netError);
//        } else {
//            showToast(R.string.ununited);
//        }
//    }

}
